from fasttext import FastText
import config


def build_classify_model(by_word=False):
    """是否是用单个字作为特征"""
    data_path = config.classify_corpus_train_path if not by_word else config.classify_corpus_by_word_train_path
    model = FastText.train_supervised(data_path, epoch=20, wordNgrams=2, minCount=1,
                                      label_prefix="__lable__")
    save_path = config.classify_model_path if not by_word else config.classify_model_path_by_word
    model.save_model(save_path)


def get_classify_model(by_word=False):
    """加载model"""
    save_path = config.classify_model_path if not by_word else config.classify_model_path_by_word
    model = FastText.load_model(save_path)
    return model


def eval(by_word):
    """模型的评估，获取模型的准确率"""
    model = get_classify_model(by_word)
    # 处理classify_text.txt分割特征和真实值

    input = []
    target = []
    eval_data_path = config.classify_corpus_test_path if not by_word else config.classify_corpus_by_word_test_path
    for line in open(eval_data_path, encoding="utf-8").readlines():
        temp = line.split("__lable__")
        if len(temp) < 2:
            continue
        input.append(temp[0].strip())
        target.append(temp[1].strip())
    # 使用特征工程和模型进行预测
    lables, acc_list = model.predict(input)
    # 计算准确率
    sum = 0
    print(len(lables), len(target))
    for i, j in zip(lables, target):
        if i[0].replace("__lable__", "") == j:
            sum += 1
    acc = sum / len(lables)  # 平均准确率
    return acc
